/**
 * 
 */
package net.synck.gcontacts.views;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import com.google.gdata.data.contacts.ContactEntry;

public class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
	private Image userImage;
	private Image deletedUserImage;
	
	public ViewLabelProvider() {
		super();
		userImage = net.synck.gcontacts.Activator.getImageDescriptor("icons/user.png").createImage();
		deletedUserImage = net.synck.gcontacts.Activator.getImageDescriptor("icons/user_gray.png").createImage();
	}
	
	public String getColumnText(Object obj, int index) {
		if (obj instanceof ContactEntry) {
			
			if (index == 0) {
				return ((ContactEntry)obj).getTitle().getPlainText();
			} else if (index == 1) {
				if ( ((ContactEntry)obj).getEmailAddresses().size() > 0 ) {
					//TODO: return primary email
					//((ContactEntry)obj).
					return ((ContactEntry)obj).getEmailAddresses().get(0).getAddress();
				} else {
					return null;
				}
				
			} else if (index == 2) {	
				if (((ContactEntry)obj).getPhoneNumbers().size() > 0) {
					//TODO: return primary phone ?
					return ((ContactEntry)obj).getPhoneNumbers().get(0).getPhoneNumber();
				} else {
					return null;
				}
			} else {
				return null; 
			}							
		} else {
			return null;
		}
		//return getText(obj);
	}
	public Image getColumnImage(Object obj, int index) {
		if (index == 0) 
			return getImage(obj);
		return null;
	}
	public Image getImage(Object obj) {
		//return PlatformUI.getWorkbench().
		//		getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
		if (obj instanceof ContactEntry) {
			if (((ContactEntry)obj).hasDeleted()) {
				return deletedUserImage;
			} else {
				return userImage;
			}
		}
		return null;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
		userImage.dispose();
		userImage = null;
		deletedUserImage.dispose();
		deletedUserImage = null;
		
	}
	
	
}